﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media.Animation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace BallMadness
{
    public class Player
    {
        public Vector2 ballPosition = Vector2.Zero;
        Vector2 ballVelocity = Vector2.Zero;
        Texture2D ball;

        int currentFrame;
        TimeSpan frameLength = new TimeSpan(0, 0, 0, 0, 150);
        TimeSpan nextFrame;

        const float ACCEL_MULTIPLIER = 4000;
        const float maxSpeed = 350;

        public int Radius { get { return ball.Height / 2; } }

        public Player()
        {
            ballPosition = new Vector2(
                SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width,
                SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height) / 2;
        }

        public void OnUpdate(GameTimerEventArgs e)
        {
            if (!ScoreManager.gameOver)
            {
                ballVelocity.X += ThunderFish.TAccelerometer.Reading.X * (float)e.ElapsedTime.TotalSeconds * ACCEL_MULTIPLIER;
                ballVelocity.Y += -ThunderFish.TAccelerometer.Reading.Y * (float)e.ElapsedTime.TotalSeconds * ACCEL_MULTIPLIER;

                if (ballVelocity.Length() > maxSpeed)
                {
                    ballVelocity.Normalize();
                    ballVelocity *= maxSpeed;
                }

                ballPosition.X += ballVelocity.X * (float)e.ElapsedTime.TotalSeconds;
                ballPosition.Y += ballVelocity.Y * (float)e.ElapsedTime.TotalSeconds;

                bounceOffBorders();

                nextFrame -= TimeSpan.FromMilliseconds(
                    (int)(e.ElapsedTime.TotalMilliseconds * (float)(ballVelocity.Length() / maxSpeed)));
                if (nextFrame <= TimeSpan.Zero)
                {
                    currentFrame++;
                    if (currentFrame > 3)
                        currentFrame = 0;
                    nextFrame = frameLength;
                }
            }
        }

        public void OnDraw(SpriteBatch spriteBatch)
        {
            if (!ScoreManager.gameOver)
            {
                spriteBatch.Draw(
                    ball,
                    ballPosition,
                    new Rectangle(
                        ball.Width / 4 * currentFrame,
                        0,
                        ball.Width / 4,
                        ball.Height),
                    Color.White,
                    (float)Math.Atan2(ballVelocity.Y, ballVelocity.X) - (float)Math.PI / 2,
                    new Vector2(ball.Width / 8, ball.Height / 2),
                    1.0f,
                    SpriteEffects.None,
                    1.0f);
            }
        }

        public void OnNavigatedTo(ContentManager contentManager)
        {
            ball = contentManager.Load<Texture2D>("bird");
        }


        public void OnNavigatedFrom()
        {
            
        }

        public void bounceOffBorders()
        {
            float borderHeight = SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height;
            float borderWidth = SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width;

            if (ballPosition.X + ball.Width / 8 > borderWidth)
            {
                ballPosition.X = borderWidth - ball.Width / 8;
                ballVelocity.X *= -1;
            }
            if (ballPosition.Y + ball.Height / 2 > borderHeight)
            {
                ballPosition.Y = borderHeight - ball.Height / 2;
                ballVelocity.Y *= -1;
            }
            if (ballPosition.X - ball.Width / 8 < 0)
            {
                ballPosition.X = ball.Width / 8;
                ballVelocity.X *= -1;
            }
            if (ballPosition.Y - ball.Height / 2 < 0)
            {
                ballPosition.Y = ball.Height / 2;
                ballVelocity.Y *= -1;
            }
        }

    }
}
